﻿  
//绑定右键菜单事件
function TabEvent() {

    //双击关闭TAB选项卡
    $(".tabs-inner").dblclick(function () {
        var subtitle = $(this).children("span").text();
        $('#tabs').tabs('close', subtitle);
    });

    //绑定右键菜单选项
    $(".tabs-inner").bind('contextmenu', function (e) {
        $('#menus').menu('show', {
            left: e.pageX,
            top: e.pageY
        });
        var subtitle = $(this).children("span").text();
        $('#menus').data("currtab", subtitle);
        return false;
    });

    //关闭当前
    $('#menu-tabclose').click(function () {
        var currtab_title = $('#menus').data("currtab");
        $('#tabs').tabs('close', currtab_title);
    });

    //全部关闭
    $('#menu-tabcloseall').click(function () {
        debugger;
        $('.tabs-closable').each(function (i, n) {
            var t = $(n).text();
            $('#tabs').tabs('close', t);
        });
    });

    //关闭除当前之外 
    $('#menu-tabcloseother').click(function () {
        var currtab_title = $('#menus').data("currtab");
        $('.tabs-inner span').each(function (i, n) {
            var t = $(n).text();
            if (t != currtab_title)
                $('#tabs').tabs('close', t);
        });
    });

    //关闭当前右侧 
    $('#menu-tabcloseright').click(function () {
        var nextall = $('.tabs-selected').nextAll();
        if (nextall.length === 0) {
            $.messager.alert('系统提示', '右侧暂无页面', 'error');
            return false;
        }
        nextall.each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            $('#tabs').tabs('close', t);
        });
        return false;
    });

    //关闭当前左侧 
    $('#menu-tabcloseleft').click(function () {
        var prevall = $('.tabs-selected').prevAll();
        if (prevall.length === 0) {
            $.messager.alert('系统提示', '左侧暂无页面', 'error');
            return false;
        }
        prevall.each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            $('#tabs').tabs('close', t);
        });
        return false;
    });

    //退出
    $("#menu-exit").click(function () {
        $('#menus').menu('hide');
    });
}

//手风琴
function AddAccordion() {

    var data = { "__RequestVerificationToken": $('[name=__RequestVerificationToken]').val() };

    $.ajax({
        url: '/EasyUI/Admin/GetAddAccordion',
        type: 'post',
        data: data,
        dataType:'json',
        success: function (result) { 
            if (result !== null && result.length > 0) {
                for (var i = 0; i < result.length; i++) {
                    var parent = "";
                    var children = "";
                    parent += "<div class='divlist'>";
                    if (result[i].children !== null && result[i].children.length > 0) {
                        for (var j = 0; j < result[i].children.length; j++)
                            children += "<div class='menuitem' urls='" + result[i].children[j].urls + "' >" + result[i].children[j].text + "</div>";
                    }
                    parent += children + "</div>";
                    $('#accordions').accordion('add', {
                        title: result[i].text,
                        selected: false,
                        content: parent
                    });
                }
            }
        }
    });
}

//添加Tab
function AddTab(subtitle, url) {
    if (!$('#tabs').tabs('exists', subtitle)) {
        $('#tabs').tabs('add', {
            title: subtitle,
            //href: '/EasyUI/DataTable', //无法跨域 
            content: '<iframe name="mainFrame" scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:95%;"></iframe>', //url页面，可跨域
            closable: true,
            width: $('#maincontext').width() - 10,
            height: $('#maincontext').height() - 26
        });
    } else {
        $('#tabs').tabs('select', subtitle);
    }
    TabEvent();  //重新绑定事件
}

//初始化窗口布局大小
function InitHeight() {
    var height = $(window).height();
    $("#layouts").layout("resize", { height: height + "px" });
}

//左侧菜单事件
$(document).on("click", ".menuitem", function () {
    AddTab($(this).html(), $(this).attr("urls")); 
});  